﻿using System;
using System.Text;
using MugenInjection.Infrastructure;
using MugenInjection.Interface;

namespace MugenInjection.Exceptions
{
    /// <summary>
    /// The exception that is thrown when the corresponding <see cref="IBinding"/> is not found.
    /// </summary>
#if !SILVERLIGHT
#if !NETFX_CORE
    [Serializable]
#endif
#endif
    public class BindingNotFoundException : BindingException
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="BindingNotFoundException"/> class with a specified error message.
        /// </summary>
        /// <param name="bindingContext">The specified <see cref="IBindingContext"/>.</param>
        public BindingNotFoundException(IBindingContext bindingContext)
            : base(FormatErrorMessage(bindingContext))
        {
        }

        #endregion

        #region Methods

        private static string FormatErrorMessage(IBindingContext bindingContext)
        {
            var sb = new StringBuilder();
            sb.AppendLine(string.Format("No matching bindings are available for a type {0}, and the type is not self-bindable.", InjectorUtilsInternal.FormatType(bindingContext.Service)));
            sb.AppendLine(string.Format("Make sure that you specify the binding for {0}.", InjectorUtilsInternal.FormatType(bindingContext.Service)));
            sb.AppendLine("The activation path:");
            sb.AppendLine(InjectorUtilsInternal.GetBuildPath(bindingContext));
            return sb.ToString();
        }

        #endregion
    }
}